package com.kun.reflection.demo02_memoryAnalysis;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

import java.lang.annotation.*;
import java.lang.reflect.Field;

//通过反射操作注解
public class Test08_operateAnnotation {

    @Test
    public void test01(){
        Class<User> c1 = User.class;
        //1. 获得注解
        Annotation[] annotations = c1.getAnnotations(); //获得所有注解
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        Table_diy annotation = c1.getAnnotation(Table_diy.class);//获得指定注解
        System.out.println(annotation.value()); //获得注解属性值

        //2. 获得字段注解
        Field[] fields = c1.getDeclaredFields();
        for (Field field : fields) {
            Field_diy annotation1 = field.getAnnotation(Field_diy.class);
            System.out.println(annotation1.name());
            System.out.println(annotation1.type());
            System.out.println(annotation1.length());
            System.out.println("-----------");
        }


    }



}


@Data
@AllArgsConstructor
@NoArgsConstructor
@Table_diy(value = "user")
class User{
    @Field_diy(name = "id", type = "varchar", length = 6)
    private String name;
    @Field_diy(name = "name", type = "int", length = 10)
    private int age;
}

//定义表注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table_diy{
    String value();
}

//定义字段注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Field_diy{
    String name();
    String type();
    int length();

}